# -*- coding: utf-8 -*-

# @Time    : 2018/12/17 16:09
# @Author  : Chen
# @File    : main.py
# @Software: PyCharm

import os
import faceDetect_OpenCV
from multiprocessing.dummy import Pool as ThreadPool
import cv2
import pandas as pd
import PIL.Image as Image

def listdir(path, list_name):  # 读取path目录及其子文件夹下所有的文件
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        else:
            list_name.append(file_path)

def listDirOnlyBmp(path, list_name): # 读取path目录及其子文件夹下所有的bmp格式文件
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        elif os.path.splitext(file_path)[1] == '.bmp':
            list_name.append(file_path)

def imageSize(path): # 获取图片大小（长，宽，通道数）
    img = cv2.imread(path)
    imgSize = img.shape # 获取图片大小（长，宽，通道数）
    return imgSize

def imageResize(path, height, width, interpolation): # resize图片
    img = Image.open(path)
    imgResize = img.resize((width, height), interpolation)
    imgResize.save(os.path.splitext(path)[0] + '_faceresize_' + os.path.splitext(path)[1])
    print('保存成功')

if __name__ == '__main__':
    originalPicPath = '..\\data\\CASIA_FACE_Data'

    # # 读取originalPicPath目录及其子文件夹下所有的bmp格式图像
    # originalPicList = []
    # listDirOnlyBmp(originalPicPath, originalPicList)
    # print(originalPicList)
    #
    # # 用OpenCV做人脸检测
    # pool = ThreadPool(4)
    # pool.map(lambda i: faceDetect_OpenCV.saveFaces(i), originalPicList)

    # # 再次读取originalPicPath目录及其子文件夹下所有的bmp格式图像，并过滤出带有'_face_'的bmp格式图像
    # originalPicList = []
    # listDirOnlyBmp(originalPicPath, originalPicList)
    # print(originalPicList)
    # listFileOnlyFace = []
    # for i in originalPicList:
    #     if '_face_' in i:
    #         listFileOnlyFace.append(i)
    # print(listFileOnlyFace)
    #
    # # 找出所有face图像的最大长与宽，以备resize使用
    # pool = ThreadPool(4)
    # imgSize = pd.DataFrame(pool.map(lambda i: imageSize(i), listFileOnlyFace), columns = ['height', 'width', 'channel_number'])
    # maxHeight = max(imgSize['height'])
    # maxWidth = max(imgSize['width'])
    #
    # # resize所有face图像（用Image包）
    # pool.map(lambda i: imageResize(i, maxHeight, maxWidth, Image.ANTIALIAS), listFileOnlyFace)

    # 再次读取originalPicPath目录及其子文件夹下所有的bmp格式图像，并过滤出带有'_faceresize_'的bmp格式图像
    originalPicList = []
    listDirOnlyBmp(originalPicPath, originalPicList)
    print(originalPicList)
    listFileOnlyFace = []
    for i in originalPicList:
        if '_faceresize_' in i:
            listFileOnlyFace.append(i)
    print(listFileOnlyFace)



    # # OpenCV图像显示
    # cv2.imshow("t1", image)
    # cv2.waitKey()
    # cv2.destroyAllWindows()
    # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 转为灰度图片
    # cv2.imshow("t2", gray)
    # cv2.waitKey()
    # cv2.destroyAllWindows()
    # # OpenCV人脸检测
    # res = faceDetect_OpenCV.detectFaces(originalPicList[0])
    # print(res)